package me.zhengjie.modules.web.rest.config;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.service.DictDetailService;
import me.zhengjie.modules.web.domain.config.RichTextReq;
import me.zhengjie.modules.web.domain.config.RichTextResp;
import me.zhengjie.modules.web.domain.config.TermsRulesReq;
import me.zhengjie.modules.web.domain.config.TermsRulesResp;
import me.zhengjie.modules.web.service.config.IConfigService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("web/config")
@Api(tags = "前端接口-配置")
public class ConfigController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private DictDetailService dictDetailService;


    @PostMapping("getRichTextByType")
    @ApiOperation("前端接口-获取富文本")
    public ResponseBaseEntity getRichTextByType(@RequestBody RichTextReq req){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isBlank(req.getRichTextType())){
                throw new WeChatException("缺少参数");
            }
            RichTextResp list = configService.getRichTextByType(req.getRichTextType());
            response.setBody(list);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @PostMapping("getRichTextListByType")
    @ApiOperation("前端接口-获取富文本")
    public ResponseBaseEntity getRichTextListByType(@RequestBody RichTextReq req){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isBlank(req.getRichTextType())){
                throw new WeChatException("缺少参数");
            }
            List<RichTextResp> list = configService.getRichTextListByType(req.getRichTextType());
            response.setBody(list);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @GetMapping("getDictValue")
    public ResponseBaseEntity getDictValue(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            String lock_user_time = dictDetailService.getDictValue(ConstantUtils.LockUserTimeDictKey);
            response.setBody(lock_user_time);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @GetMapping("getDictValueByName")
    public ResponseBaseEntity getDictValue(String name){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            String value = dictDetailService.getDictValue(name);
            response.setBody(value);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @RequestMapping("getTermsRules")
    public ResponseBaseEntity getTermsRules(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<TermsRulesResp> value = configService.getTermsRulesList();
            response.setBody(value);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @RequestMapping("getTermsRulesV2")
    public ResponseBaseEntity getTermsRulesV2(@RequestBody TermsRulesReq req){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (req.getType() == null){
                throw new WeChatException("缺少参数");
            }
            List<TermsRulesResp> value = configService.getTermsRulesListV2(req.getType());
            response.setBody(value);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @RequestMapping("getTermsRulesByCode")
    public ResponseBaseEntity getTermsRulesByCode(@RequestBody TermsRulesReq req){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isBlank(req.getKey())){
                throw new WeChatException("缺少参数");
            }
            TermsRulesResp value = configService.getTermsRulesByCode(req.getKey());
            response.setBody(value);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
